package com.example.a33mvvm.login;

import androidx.lifecycle.Observer;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.a33mvvm.MainActivity;
import com.example.a33mvvm.R;

import com.example.a33mvvm.databinding.ActivityLoginBinding;
import com.example.a33mvvm.register.RegisterActivity;
import com.example.base_mvvm.base.view.BaseView;
import com.example.base_mvvm.money.AppDataBaseUtils;
import com.example.base_mvvm.money.Money;
import com.example.base_mvvm.net.Const;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

public class LoginActivity extends BaseView<ActivityLoginBinding,LoginViewModel> {

    @Override
    protected void initData() {
        viewModel.success.observe(this, loginEntity -> {
            if(loginEntity.getCode() == 200){
                Money money = new Money(0,loginEntity.getData().getUsername(),999999L);
                ToastUtils.showLong("欢迎回来"+loginEntity.getData().getUsername());
                AppDataBaseUtils.getInstance().getMoneyDao().insert(money);
                SPUtils.getInstance().put(Const.SP_USERNAME,loginEntity.getData().getUsername());
                SPUtils.getInstance().put(Const.SP_TOKEN,loginEntity.getData().getToken());

                EMClient.getInstance().login(loginEntity.getData().getUsername(), loginEntity.getData().getPassword(), new EMCallBack() {
                    @Override
                    public void onSuccess() {
                        Log.i("TAG", "onSuccess: "+"登录成功");
                    }

                    @Override
                    public void onError(int code, String error) {
                        Log.i("TAG", "onError:.................................................................."+"登录失败"+error);
                    }
                });
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
            }else{
                ToastUtils.showLong(loginEntity.getMessage());
            }
        });
        viewModel.failure.observe(this, s -> ToastUtils.showLong(s));
    }

    @Override
    protected void initView() {
       dataBinding.loginLogin.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               String username = dataBinding.loginUsername.getText().toString().trim();
               String password = dataBinding.loginPassword.getText().toString().trim();
               if(TextUtils.isEmpty(username)|| TextUtils.isEmpty(password)){
                   ToastUtils.showLong("请输入账号密码");
               }else{
                   viewModel.login(username, password);
               }
           }
       });
       dataBinding.loginToRegister.setOnClickListener(view -> {
           Intent intent = new Intent(this, RegisterActivity.class);
           startActivity(intent);
       });
       dataBinding.loginLogout.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               EMClient.getInstance().logout(true);
           }
       });
        getWindow().setStatusBarColor(Color.TRANSPARENT);
    }
    @Override
    protected Class<LoginViewModel> getViewModel() {
        return LoginViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }
}